{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "586d5465",
   "metadata": {},
   "source": [
    "#   API 文档介绍（Face++ API）\n",
    "* 本周主要内容：图像识别（认知服务）\n",
    "* 22春_API_人工智能与机器学习_week06"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "905705e6",
   "metadata": {},
   "source": [
    "#  百度AI开放平台\n",
    "## 通用物体和场景识别\n",
    "* 接口描述\n",
    "---\n",
    "该请求用于通用物体及场景识别，即对于输入的一张图片（可正常解码，且长宽比适宜），输出图片中的多个物体及场景标签。\n",
    "\n",
    "* 请求示例\n",
    "---\n",
    "HTTP 方法：POST\n",
    "\n",
    "* 请求URL\n",
    "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\n",
    "\n",
    "## 鉴权认证机制(access_token)\n",
    "* 获取Access Token\n",
    "* 获取access_token示例代码:\n",
    "\n",
    "```\n",
    "\n",
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "870af49f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.41f94f5661a6e26f81797634a209d37d.315360000.1965023018.282335-25904832', 'expires_in': 2592000, 'session_key': '9mzdX+PRVt2Z+XTShOZYN9gTkBPf4Ij/6IibDfFS5hFo1bLBcCQ6aziJRwyb66Vsqxq/8IUDbMEoTsSIciA2qbUuHzjALA==', 'access_token': '24.d9e48ea06bf3c5b2ad42ce3fb645efa2.2592000.1652255018.282335-25904832', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': '342c2d9541b960b55225fae00d20759c'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'ilIoXKonMCenk3VGQrpzlyLX',\n",
    "    'client_secret':'GUdk32pHkHkV1G7MHKIDEO1NoeFsZwOo'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "beacc5ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "qiuzhen_API = response.json()['access_token']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fc6888c",
   "metadata": {},
   "source": [
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3e5155bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1e97e412",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>参数</th>\n",
       "      <th>是否必选</th>\n",
       "      <th>类型</th>\n",
       "      <th>可选值范围</th>\n",
       "      <th>说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>image</td>\n",
       "      <td>和url二选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图像数据，base64编码，要求base64编码后大小不超过4M，最短边至少15px，最长边...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>url</td>\n",
       "      <td>和image二选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>baike_num</td>\n",
       "      <td>否</td>\n",
       "      <td>integer</td>\n",
       "      <td>-</td>\n",
       "      <td>返回百科信息的结果数，默认不返回</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          参数       是否必选       类型 可选值范围  \\\n",
       "0      image    和url二选一   string     -   \n",
       "1        url  和image二选一   string     -   \n",
       "2  baike_num          否  integer     -   \n",
       "\n",
       "                                                  说明  \n",
       "0  图像数据，base64编码，要求base64编码后大小不超过4M，最短边至少15px，最长边...  \n",
       "1  图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4...  \n",
       "2                                   返回百科信息的结果数，默认不返回  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 图像识别 url ： https://ai.baidu.com/ai-doc/IMAGERECOGNITION/Xk3bcxe21\n",
    "pd.read_html('https://ai.baidu.com/ai-doc/IMAGERECOGNITION/Xk3bcxe21')[2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02d6cebc",
   "metadata": {},
   "source": [
    "## 示例代码\n",
    "```\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('[本地文件]', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '[调用鉴权接口获取的token]'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a1385187",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'keyword': '树', 'score': 0.235827, 'root': '植物-树', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E6%A0%91/2699484', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/6159252dd42a2834218a2c2154b5c9ea15cebfef', 'description': '树是一种数据结构，它是由n(n≥0)个有限节点组成一个具有层次关系的集合。把它叫做“树”是因为它看起来像一棵倒挂的树，也就是说它是根朝上，而叶朝下的。它具有以下的特点：每个节点有零个或多个子节点；没有父节点的节点称为根节点；每一个非根节点有且只有一个父节点；除了根节点外，每个子节点可以分为多个不相交的子树。'}}, {'keyword': '传统建筑', 'score': 0.211075, 'root': '建筑-现代建筑', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E4%BC%A0%E7%BB%9F%E5%BB%BA%E7%AD%91/3098139', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/a08b87d6277f9e2f81fab9931f30e924b999f35a', 'description': '《传统建筑》是2010年1月东方出版社出版的图书，作者是宋文。本书通过回顾中国传统建筑历史及其发展过程，对传统建筑中常见的建筑形态做了详细介绍，并配以详细图片进行解说，以帮助读者更加直观、具体地了解中国传统建筑的各个方面。'}}, {'keyword': '亭楼', 'score': 0.155967, 'root': '建筑-传统建筑', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E4%BA%AD%E6%A5%BC/22786973', 'description': '亭楼(拉丁语：papilio；法语：pavillon；英语：pavilion)是一种由立柱支撑的中空建筑。语源出自拉丁语papilio，系由鳞翅目昆虫(蝶、蛾)翅膀的状态隐喻而来。亭楼从临时搭建的帐篷发展而成，作为古代的建筑遗迹，一般可供人休憩和观赏用；作为现代建筑则突出其展览意义，如博览会上的各个展馆。广义上，它也包括了类似的亚洲建筑，如东亚传统样式亭子以及伊斯兰世界的凉亭等。'}}, {'keyword': '街道', 'score': 0.082057, 'root': '建筑-其他', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E8%A1%97%E9%81%93/419541', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/7aec54e736d12f2eb9380a579793c2628535e5ddc2f0', 'description': '街道，是中国行政区划之一，行政地位与镇、乡、民族乡、苏木、民族苏木、县辖区相同，属乡级行政区，由市辖区、县级市、县、自治县、旗、特区管辖或由地级市直接管辖。街道的管理机构为街道办事处，是市辖区、县级市、县、自治县、旗、特区,地级市人民政府的派出机关。截止到2019年12月底，中国共计8516个街道。截至2020年底，中国共有街道8773个。'}}, {'keyword': '天空', 'score': 0.005517, 'root': '自然风景-天空', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E5%A4%A9%E7%A9%BA/5534', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/241f95cad1c8a7868fa019076909c93d70cf5034', 'description': '天空，汉语词汇，拼音tiān kōng，谓天际空阔；日月星辰罗列的广大空间。是地球重要的组成部分，在古代中国有天圆地方的说法。人可以在天空观测气象或天文现象，从而得知天气变化、时间的流逝或自己的方位。日出日落可知一日中的时间，晚上月亮的盈亏可以知道一个月的时间。北斗星可以指示北方。云的厚度和形状可以知道会否下雨。 在天空可以欣赏到许多美丽的现象，如彩虹、极光和流星雨等。雀鸟会在天空飞翔。 由于石油等化石性燃料使用的增加而产生的悬浮质，特别是那些会在燃烧后释放二氧化硫的煤等燃料的影响，自1973年以来，除了欧洲，天空的能见度正在逐步降低。'}}], 'log_id': 1513429570929847878}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('xihu.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "payload={\n",
    "    'access_token':qiuzhen_API,\n",
    "    'image':img,\n",
    "    'baike_num':5\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1fe1303",
   "metadata": {},
   "source": [
    "## 菜品识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c9b65c5b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'calorie': '160', 'has_calorie': True, 'name': '口水鸡', 'probability': '0.430561', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E5%8F%A3%E6%B0%B4%E9%B8%A1/120873', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/eac4b74543a98226cffc97d5d7c9ae014a90f703bda4', 'description': '口水鸡是四川省达州市的一道传统名菜，属于川菜，该菜品佐料丰富，集麻辣鲜香嫩爽于一身，有“名驰巴蜀三千里，味压江南十二州”的美称。 在烹制时，煮鸡用的汤料很有讲究，需要恰到好处，这样可以最大限度地保存鸡的可溶性蛋白，增加鸡肉的鲜美程度，又能具备其特有的香型和滋味。'}}, {'calorie': '185', 'has_calorie': True, 'name': '葱油鸡', 'probability': '0.290971', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E8%91%B1%E6%B2%B9%E9%B8%A1/2892207', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/0823dd54564e9258cb29cf839282d158ccbf4eb9', 'description': '据查葱油鸡是广东省的传统名菜，属于粤菜系。此菜用材简单，制作方便，滑润细嫩，清鲜醇厚，葱香四溢。很适合春夏季食用。把将鸡里外充分洗净，生姜拍破，用3根葱打成结，把大葱切段对剖，香葱切成段，一点葱叶切成花，在深锅内烧宽水，加入生姜、葱、花椒煮开后放入鸡，加入一点黄酒，大火再次烧开后，打出表面的浮沫，以保持沸腾状态的小火煮10分钟(中间翻面)，关火，加盖子焖10分钟后捞出，将鸡斩成块，热锅温花生油，以中小火把葱煎出香味后捞出，倒入蒸鱼豉油，熬1分钟后关火，做成味汁，将味汁充分搅匀后，趁热淋在鸡块上，撒上一点儿葱花，即成。'}}, {'calorie': '188', 'has_calorie': True, 'name': '白切鸡', 'probability': '0.205162', 'baike_info': {}}, {'calorie': '166', 'has_calorie': True, 'name': '三黄鸡', 'probability': '0.0166165', 'baike_info': {}}, {'calorie': '158', 'has_calorie': True, 'name': '水晶鸡', 'probability': '0.00838958', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E6%B0%B4%E6%99%B6%E9%B8%A1/3531891', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/9345d688d43f8794cbae2312d11b0ef41bd53abc', 'description': '水晶鸡是一道广东湛江的特色传统名菜，属于粤菜系。始创于清朝名厨陈柳歧先生，祖籍湛江，陈柳歧先生师从祖父，自幼爱吃鸡，故对鸡的食法及煮法有独到心得，并独创一种腌鸡的专用配料，用于鸡的腌制及蘸料；通过几代传人的传播，现成为湛江地区特色名菜——湛江水晶鸡(又名：隔水蒸鸡、清香鸡、古典鸡、桶皇鸡)。'}}], 'log_id': 1513432338431039288}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "菜品识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/dish\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('baiqieji.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "access_token = qiuzhen_API\n",
    "payload={\n",
    "    'access_token':qiuzhen_API,\n",
    "    'image':img,\n",
    "    'baike_num':5,\n",
    "    \"top_num\":5\n",
    "}\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e677906b",
   "metadata": {},
   "source": [
    "## 植物识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e76e8019",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': [{'name': '玫瑰', 'score': 0.54682577, 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E7%8E%AB%E7%91%B0/63206', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/b7003af33a87e950352a4e90ad764443fbf2b3111eb2', 'description': '玫瑰(学名：Rosa ssp.英文名称：Rosa)：是蔷薇目，蔷薇科、蔷薇属多种植物和培育花卉的通称名字。直立、蔓延或攀援灌木，多数被有皮刺、针刺或刺毛，稀无刺，有毛、无毛或有腺毛。叶互生；花单生；花托球形、坛形至杯形；花瓣5，稀4，开展，覆瓦状排列，白色、黄色，粉红色至红色；花柱顶生至侧生，外伸，离生或上部合生；花瓣倒卵形，重瓣至半重瓣，花有紫红色、黄色、粉色、白色和各种复色。枝条较为柔弱软垂且多密刺，每年花期只有一次。玫瑰是所有花卉中最著名和最受欢迎的一类。几个世纪以来，玫瑰一直备受推崇。历史证据表明，它们大约在5000年前在中国生长，从那时起它们就一直在历史中发挥作用。无论是古希腊人、罗马人、基督徒、共济会或其他人，玫瑰始终是爱、美和平等的永恒象征。虽然每种颜色表达爱，美和平衡有点不同，但主要的信息仍然是爱。玫瑰有许多不同的含义，这取决于它的颜色。然而，任何玫瑰通常都可以被视为象征：爱、荣誉、信仰、美丽、平衡、热情、智慧、奉献和永恒。玫瑰作为经济作物时，其花朵主要用于食品及提炼香精玫瑰油，玫瑰油应用于化妆品、食品、精细化工等工业。在欧洲诸语言中，蔷薇、玫瑰、月季都是使用同一个词，如英语是rose，德语是Die Rose。玫瑰是英国、美国等14个国家的国花。通俗意义中的“玫瑰”已成为多种蔷薇属植物的通称。且事实上杂交玫瑰也是由蔷薇属下各物种杂交选育所产生。'}}, {'name': '月季花', 'score': 0.53668416, 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E6%9C%88%E5%AD%A3%E8%8A%B1/14505544', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/562c11dfa9ec8a139fe24202f903918fa1ecc05c', 'description': '月季花(学名：Rosa chinensis Jacq.)：被称为花中皇后，又称“月月红”，是常绿、半常绿低矮灌木，四季开花﹐一般为红色﹐或粉色、偶有白色和黄色﹐可作为观赏植物，也可作为药用植物，亦称月季。有三个自然变种，现代月季花型多样，有单瓣和重瓣，还有高心卷边等优美花型；其色彩艳丽、丰富，不仅有红、粉、黄、白等单色，还有混色、银边等品种；多数品种有芳香。月季的品种繁多，世界上已有近万种，中国也有千种以上。自然花期4月-9月，花成大型，由内向外，呈发散型，有浓郁香气，可广泛用于园艺栽培和切花。月季的适应性强，耐寒、抗旱，不论地栽、盆栽均可，适用于美化庭院、装点园林、布置花坛、配植花篱、花架，月季栽培容易，可作切花，用于做花束和各种花篮，月季花朵可提取香精，并可入药。也有较好的抗真菌及协同抗耐药真菌活性。红色切花更成为情人间必送的礼物之一，并成为爱情诗歌的主题。中国是月季的原产地之一。月季花荣秀美，姿色多样，四时常开，深受人们的喜爱，中国有52个城市将它选为市花，1985年5月月季被评为中国十大名花第五名。(概述图片参考资料来源：)'}}, {'name': '丰花月季', 'score': 0.0061995634, 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E4%B8%B0%E8%8A%B1%E6%9C%88%E5%AD%A3/3031518', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/35a85edf8db1cb13495459aaeb1d414e9258d109dd7b', 'description': '丰花月季(学名：Rosa hybrida E.H.L.Krause)是蔷薇科、蔷薇属植物。灌木，高0.9-1.3米，小枝具钩刺或无刺、无毛，羽状复叶，小叶5-7片，宽卵形或卵状长圆形，长2.3-6.0厘米，先端渐尖，基部近圆形或宽楔形。具尖锯齿，无毛。花单生或几朵集生，呈伞房状，花径4-6厘米，花梗3-5厘米，常被腺毛，萼片卵形，花瓣有深红、银粉、淡粉、黑红、橙黄等颜色，重瓣，花柱分离，子房被柔毛，蔷薇果卵球形，径1.0-1.2厘米，红色，花期5月底至11月初，果期9-11月。主要分布中国华北南部，西北、华中、华南等省(区)。对环境适应性强，对土壤要求不严，但在土壤肥沃、排水良好的酸性土壤上生长最好。丰花月季多分枝，呈较矮的灌丛状，具有梗长、花美、耐寒、耐热、花团锦簇等优点，且新叶和秋叶红艳，非常适宜装饰街心、道旁，作沿墙的花篱，独立的画屏或花圃的镶边。又可按几何图案布置成规则式的花坛、花带，还可与其他品种共同构成内容丰富的月季园以供欣赏。(概述图参考来源：)'}}, {'name': '洋桔梗', 'score': 0.004506244, 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E6%B4%8B%E6%A1%94%E6%A2%97/6946300', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/00e93901213fb80e8fead6a53ed12f2eb938948d', 'description': '洋桔梗(Eustoma grandiflorum(Raf.)Shinners)龙胆科多年生植物，株高30-100厘米。叶对生，阔椭圆形至披针形，几无柄，叶基略抱茎；叶表蓝绿色。雌雄蕊明显，苞片狭窄披针形，花瓣覆瓦状排列。花色丰富，有单色及复色，花瓣单瓣与双瓣之分。洋桔梗又名草原龙胆，原生于美国南部至墨西哥之间的石灰岩地带，现代的洋桔梗栽培是在日本兴旺起来的。在20世纪60年代已开始作商业化的栽培。洋桔梗花色典雅明快，花形别致可爱，是国际上十分流行的盆花和切花种类之一。(概述图片来源：)'}}, {'name': '当代月季', 'score': 0.0032718254, 'baike_info': {}}], 'log_id': 1513433742760730462}\n"
     ]
    }
   ],
   "source": [
    "## \n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "植物识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/plant\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('meigui.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "payload={\n",
    "    'access_token':qiuzhen_API,\n",
    "    'image':img,\n",
    "    'baike_num':5,\n",
    "    \"top_num\":5\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad538e00",
   "metadata": {},
   "source": [
    "# 作业：尝试使用百度API图像识别其中一个\n",
    "* 做用户需求分析并设计一个最小可行性应用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e475db3a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
